﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace LectorHuellaC
{
   
    public partial class FormProductos : Form
    {
      
        log4net.ILog logger;
        cls_category cls_category = new cls_category();
        cls_productos cls_product = new cls_productos();
        public FormProductos()
        {
            log4net.Config.XmlConfigurator.Configure();
            logger = log4net.LogManager.GetLogger(typeof(Program));
            InitializeComponent();
            pintarProductos();
            getCategories();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtnomproducto.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Campo nombre no puede estar vacion", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                    
                int id_producto=0;
                if (!chknuevoproducto.Checked)
                {
                    id_producto = Convert.ToInt32(PrintCellRowAndCol());
                    
                }
                string nombre = txtnomproducto.Text;
                int category_id = Convert.ToInt32(cbcategoria.SelectedValue);
                string product_code = txbcodigop.Text;
                string description = txadescrip.Text;

                bool resp = cls_product.insertarActualizarProductos(id_producto, nombre, category_id, product_code, description);
                if (resp)
                {
                    MessageBox.Show("Se realizo la insercion", "Insercion productos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    pintarProductos();
                    limpiarControles();
                }
                else
                {
                    MessageBox.Show("Se ha presentado un problema", "Insercion productos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    limpiarControles();
                }
            }catch (Exception ex)
            {
                logger.Error("Error"+ex.Message); 
            }
        }

        public int PrintCellRowAndCol()
        {
            // Prints the value of the cell through the DataTable.
            DataTable myTable;
            // Assumes the DataGrid is bound to a DataTable.
            myTable = (DataTable)dataGridView1.DataSource;
            int id_producto = 0;
            if (dataGridView1.Rows.Count != 0)
            {
                DataGridViewCell myCell;
                myCell = dataGridView1.CurrentCell;
                if (myCell!=null)
                {
                    if (myCell.Selected)
                    {
                   
                        //Object xxx = myTable.Rows[myCell.RowIndex][myCell.ColumnIndex];
                        DataRow values = myTable.Rows[myCell.RowIndex];

                        id_producto = Convert.ToInt32(values[0]);
                    }
                }
            }
            return id_producto;
        }

        public void limpiarControles()
        {
            txadescrip.Text ="";
            txbcodigop.Text ="";
            txtnomproducto.Text = "";
        }

        public void getCategories ()
        {
            DataTable categories = cls_category.getCategories();
           
            ArrayList arreglo = new ArrayList();
            foreach (DataRow categoryrow in categories.Rows)
            {
                arreglo.Add(new Entidades.Categories(Convert.ToInt32(categoryrow["category_id"].ToString()), categoryrow["category_name"].ToString()));
            }
            cbcategoria.DataSource = arreglo;
            cbcategoria.DisplayMember = "_category_name";
            cbcategoria.ValueMember = "_category_id";
            cbcategoria_SelectedIndexChanged(cbcategoria, System.EventArgs.Empty);

            cbcategoriasearch.DataSource = arreglo;
            cbcategoriasearch.DisplayMember = "_category_name";
            cbcategoriasearch.ValueMember = "_category_id";
            cbcategoriasearch_SelectedIndexChanged(cbcategoria, System.EventArgs.Empty);
        }

        private void cbcategoriasearch_SelectedIndexChanged(ComboBox cbcategoria, EventArgs eventArgs)
        {
            
        }

        private void cbcategoria_SelectedIndexChanged(ComboBox cbcategoria, EventArgs eventArgs)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Dispose();
            Close();
        }





        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow row = dataGridView1.CurrentRow;

            if (!string.IsNullOrEmpty(Convert.ToString(row.Cells["product_id"].Value)))
            {

                DataTable myTable;
                myTable = (DataTable)dataGridView1.DataSource;

                DataGridViewCell myCell;
                myCell = dataGridView1.CurrentCell;


                DataRow values = myTable.Rows[myCell.RowIndex];

                txtnomproducto.Text = Convert.ToString(values["product_name"]);
                txbcodigop.Text = Convert.ToString(values["product_code"]);
                txadescrip.Text = Convert.ToString(values["product_description"]);
            }
            else
            {
                limpiarControles();
            }
        }

        public void pintarProductos()
        {
            dataGridView1.DataSource = cls_product.getProductInfo(0, 0);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string search = "";
            int category_id = Convert.ToInt32(cbcategoria.SelectedValue);
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                search=txtbsearch.Text;
                dataGridView1.DataSource = cls_product.getProductInfoByCriteria(search, category_id);
            }
        }


        }

    }

